package com.tanhua.management.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.dubbo.server.api.ManageMomentsApi;
import com.tanhua.dubbo.server.pojo.*;
import com.tanhua.dubbo.server.vo.PageInfo;
import com.tanhua.management.mapper.UserInfoMapper;
import com.tanhua.management.pojo.UserInfo;
import com.tanhua.management.vo.DetailsVo;
import com.tanhua.management.vo.MomentsVo;
import com.tanhua.management.vo.PageResult;
import com.tanhua.management.vo.TotalsVo;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class ManageCommentsService {
	
	
	@Reference(version = "1.0.0")
	private ManageMomentsApi manageCommentsApi;
	
	@Autowired
	private UserInfoMapper userInfoMapper;
	
	@Autowired
	private MongoTemplate mongoTemplate;
	
	
	// 没有传递uid和创建时间范围时的查询
	public PageResult queryMomentsList(Integer page, Integer pageSize, String sortProp, String sortOrder, String code) {
		PageResult pageResult = new PageResult();
		pageResult.setPage(page);
		pageResult.setPageSize(pageSize);
		PageInfo<Moments> pageInfo = null;
		
		if (code.equals("all")) {
			// 查询所有状态的数据
			pageInfo = this.manageCommentsApi.queryMomentsListAll(page, pageSize, sortProp, sortOrder);
		}
		
		if (code.equals("3")) {
			// 查询状态为人工审核的数据
			pageInfo = this.manageCommentsApi.queryMomentsListReview(page, pageSize, sortProp, sortOrder);
		}
		
		if (code.equals("4")) {
			// 查询状态为拒绝的数据
			pageInfo = this.manageCommentsApi.queryMomentsListReject(page, pageSize, sortProp, sortOrder);
		}
		
		if (code.equals("5")) {
			// 查询状态为通过的数据
			pageInfo = this.manageCommentsApi.queryMomentsListPass(page, pageSize, sortProp, sortOrder);
		}
		
		// 获取到动态publish集合,遍历强化封装成moment
		List<Moments> records = pageInfo.getRecords();
		List<MomentsVo> momentsVos = new ArrayList<>();
		for (Moments record : records) {
			MomentsVo momentsVo = new MomentsVo();
			momentsVo.setUserId(record.getUserId());  // 设置用户ID
			// List<String> medias = record.getMedias();
			momentsVo.setCreateDate(record.getCreateDate()); // 设置动态创建时间
			momentsVo.setMedias(record.getMedias());  // 设置medias
			momentsVo.setText(record.getText()); // 设置文本内容
			
			if (record.getState().equals("review")) {
				momentsVo.setState("3");
			}
			if (record.getState().equals("pass") || record.getState().equals("auto_pass")) {
				momentsVo.setState("5");
			}
			if (record.getState().equals("auto_reject") || record.getState().equals("reject")) {
				momentsVo.setState("4");
			}
			
			// 获得相应id用户的详细信息,进行设置头像和昵称
			QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
			queryWrapper.eq("user_id", record.getUserId());
			UserInfo userInfo = this.userInfoMapper.selectOne(queryWrapper);
			
			momentsVo.setUserLogo(userInfo.getLogo()); // 设置头像
			momentsVo.setNickname(userInfo.getNickName()); // 设置昵称
			
			momentsVo.setId(record.getOid().toHexString()); // 设置动态ID
			momentsVo.setReportCount(RandomUtils.nextInt(50, 80)); // 设置举报数量
			momentsVo.setLikeCount(RandomUtils.nextInt(50, 500)); // 设置点赞数量
			momentsVo.setCommentCount(RandomUtils.nextInt(0, 50)); // 设置评论数量
			momentsVo.setForwardingCount(RandomUtils.nextInt(0, 50)); // 设置转发数量
			
			momentsVos.add(momentsVo);
		}
		
		if (pageInfo.getTotal() % pageInfo.getPageSize() == 0) {
			pageResult.setPages(pageInfo.getTotal() / pageInfo.getPageSize());
		} else {
			pageResult.setPages(pageInfo.getTotal() / pageInfo.getPageSize() + 1);
		}
		
		pageResult.setCounts(pageInfo.getTotal());
		
		pageResult.setItems(momentsVos);
		
		return pageResult;
	}
	
	// 获取totalsVo集合的对象
	public List<TotalsVo> queryTotalsVos(Integer page, Integer pageSize, String sortProp, String sortOrder) {
		List<TotalsVo> totalsVos = new ArrayList<>();
		
		TotalsVo totalsVo = new TotalsVo();
		TotalsVo totalsVo1 = new TotalsVo();
		TotalsVo totalsVo2 = new TotalsVo();
		TotalsVo totalsVo3 = new TotalsVo();
		
		PageInfo<Moments> pageInfo = this.manageCommentsApi.queryMomentsListAll(page, pageSize, sortProp, sortOrder);
		totalsVo.setTitle("全部");
		totalsVo.setCode("all");
		totalsVo.setValue(pageInfo.getTotal());
		
		PageInfo<Moments> pageInfo1 = this.manageCommentsApi.queryMomentsListReview(page, pageSize, sortProp, sortOrder);
		totalsVo1.setTitle("待审核");
		totalsVo1.setCode("3");
		totalsVo1.setValue(pageInfo1.getTotal());
		
		PageInfo<Moments> pageInfo2 = this.manageCommentsApi.queryMomentsListReject(page, pageSize, sortProp, sortOrder);
		totalsVo2.setTitle("已驳回");
		totalsVo2.setCode("4");
		totalsVo2.setValue(pageInfo2.getTotal());
		
		PageInfo<Moments> pageInfo3 = this.manageCommentsApi.queryMomentsListPass(page, pageSize, sortProp, sortOrder);
		totalsVo3.setTitle("已通过");
		totalsVo3.setCode("5");
		totalsVo3.setValue(pageInfo3.getTotal());
		
		totalsVos.add(totalsVo);
		totalsVos.add(totalsVo1);
		totalsVos.add(totalsVo2);
		totalsVos.add(totalsVo3);
		
		return totalsVos;
	}
	
	// 传递uid没有传递创建时间范围时的查询
	public PageResult queryMomentsListByUid(Integer page, Integer pageSize, String sortProp, String sortOrder, String code, String uid) {
		PageResult pageResult = new PageResult();
		pageResult.setPage(page);
		pageResult.setPageSize(pageSize);
		PageInfo<Moments> pageInfo = null;
		
		if (code.equals("all") || StringUtils.isEmpty(code)) {
			// 查询所有状态的数据
			pageInfo = this.manageCommentsApi.queryMomentsListAllByUid(page, pageSize, sortProp, sortOrder, uid);
		}
		
		if (code.equals("3")) {
			// 查询状态为人工审核的数据
			pageInfo = this.manageCommentsApi.queryMomentsListReviewByUid(page, pageSize, sortProp, sortOrder, uid);
		}
		
		if (code.equals("4")) {
			// 查询状态为拒绝的数据
			pageInfo = this.manageCommentsApi.queryMomentsListRejectByUid(page, pageSize, sortProp, sortOrder, uid);
		}
		
		if (code.equals("5")) {
			// 查询状态为通过的数据
			pageInfo = this.manageCommentsApi.queryMomentsListPassByUid(page, pageSize, sortProp, sortOrder, uid);
		}
		
		// 获取到动态publish集合,遍历强化封装成moment
		List<Moments> records = pageInfo.getRecords();
		List<MomentsVo> momentsVos = new ArrayList<>();
		for (Moments record : records) {
			MomentsVo momentsVo = new MomentsVo();
			momentsVo.setUserId(record.getUserId());  // 设置用户ID
			// List<String> medias = record.getMedias();
			momentsVo.setCreateDate(record.getCreateDate()); // 设置动态创建时间
			momentsVo.setMedias(record.getMedias());  // 设置medias
			momentsVo.setText(record.getText()); // 设置文本内容
			
			if (record.getState().equals("review")) {
				momentsVo.setState("3");
			}
			if (record.getState().equals("pass") || record.getState().equals("auto_pass")) {
				momentsVo.setState("5");
			}
			if (record.getState().equals("auto_reject") || record.getState().equals("reject")) {
				momentsVo.setState("4");
			}
			
			// 获得相应id用户的详细信息,进行设置头像和昵称
			QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
			queryWrapper.eq("user_id", record.getUserId());
			UserInfo userInfo = this.userInfoMapper.selectOne(queryWrapper);
			
			momentsVo.setUserLogo(userInfo.getLogo()); // 设置头像
			momentsVo.setNickname(userInfo.getNickName()); // 设置昵称
			
			momentsVo.setId(record.getOid().toHexString()); // 设置动态ID
			momentsVo.setReportCount(RandomUtils.nextInt(50, 80)); // 设置举报数量
			momentsVo.setLikeCount(RandomUtils.nextInt(50, 500)); // 设置点赞数量
			momentsVo.setCommentCount(RandomUtils.nextInt(0, 50)); // 设置评论数量
			momentsVo.setForwardingCount(RandomUtils.nextInt(0, 50)); // 设置转发数量
		}
		
		if (pageInfo.getTotal() % pageInfo.getPageSize() == 0) {
			pageResult.setPages(pageInfo.getTotal() / pageInfo.getPageSize());
		} else {
			pageResult.setPages(pageInfo.getTotal() / pageInfo.getPageSize() + 1);
		}
		
		pageResult.setCounts(pageInfo.getTotal());
		
		pageResult.setItems(momentsVos);
		
		return pageResult;
	}
	
	public PageResult queryMomentsListByUidAndData(Integer page, Integer pageSize, String sortProp, String sortOrder, String code, String uid, Long start, Long end) {
		PageResult pageResult = new PageResult();
		pageResult.setPage(page);
		pageResult.setPageSize(pageSize);
		PageInfo<Moments> pageInfo = null;
		
		if (code.equals("all")) {
			// 查询所有状态的数据
			pageInfo = this.manageCommentsApi.queryMomentsListAllByUidAndData(page, pageSize, sortProp, sortOrder, uid, start, end);
		}
		
		if (code.equals("3")) {
			// 查询状态为人工审核的数据
			pageInfo = this.manageCommentsApi.queryMomentsListReviewByUidAndData(page, pageSize, sortProp, sortOrder, uid, start, end);
		}
		
		if (code.equals("4")) {
			// 查询状态为拒绝的数据
			pageInfo = this.manageCommentsApi.queryMomentsListRejectByUidAndData(page, pageSize, sortProp, sortOrder, uid, start, end);
		}
		
		if (code.equals("5")) {
			// 查询状态为通过的数据
			pageInfo = this.manageCommentsApi.queryMomentsListPassByUidAndData(page, pageSize, sortProp, sortOrder, uid, start, end);
		}
		
		// 获取到动态publish集合,遍历强化封装成moment
		List<Moments> records = pageInfo.getRecords();
		List<MomentsVo> momentsVos = new ArrayList<>();
		for (Moments record : records) {
			MomentsVo momentsVo = new MomentsVo();
			momentsVo.setUserId(record.getUserId());  // 设置用户ID
			// List<String> medias = record.getMedias();
			momentsVo.setCreateDate(record.getCreateDate()); // 设置动态创建时间
			momentsVo.setMedias(record.getMedias());  // 设置medias
			momentsVo.setText(record.getText()); // 设置文本内容
			
			if (record.getState().equals("review")) {
				momentsVo.setState("3");
			}
			if (record.getState().equals("pass") || record.getState().equals("auto_pass")) {
				momentsVo.setState("5");
			}
			if (record.getState().equals("auto_reject") || record.getState().equals("reject")) {
				momentsVo.setState("4");
			}
			
			// 获得相应id用户的详细信息,进行设置头像和昵称
			QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
			queryWrapper.eq("user_id", record.getUserId());
			UserInfo userInfo = this.userInfoMapper.selectOne(queryWrapper);
			
			momentsVo.setUserLogo(userInfo.getLogo()); // 设置头像
			momentsVo.setNickname(userInfo.getNickName()); // 设置昵称
			
			momentsVo.setId(record.getOid().toHexString()); // 设置动态ID
			momentsVo.setReportCount(RandomUtils.nextInt(50, 80)); // 设置举报数量
			momentsVo.setLikeCount(RandomUtils.nextInt(50, 500)); // 设置点赞数量
			momentsVo.setCommentCount(RandomUtils.nextInt(0, 50)); // 设置评论数量
			momentsVo.setForwardingCount(RandomUtils.nextInt(0, 50)); // 设置转发数量
		}
		
		if (pageInfo.getTotal() % pageInfo.getPageSize() == 0) {
			pageResult.setPages(pageInfo.getTotal() / pageInfo.getPageSize());
		} else {
			pageResult.setPages(pageInfo.getTotal() / pageInfo.getPageSize() + 1);
		}
		
		pageResult.setCounts(pageInfo.getTotal());
		
		pageResult.setItems(momentsVos);
		
		return pageResult;
	}
	
	public PageResult queryMomentsListByUidAndDataByData(Integer page, Integer pageSize, String sortProp, String sortOrder, String code, Long start, Long end) {
		PageResult pageResult = new PageResult();
		pageResult.setPage(page);
		pageResult.setPageSize(pageSize);
		PageInfo<Moments> pageInfo = null;
		
		if (code.equals("all")) {
			// 查询所有状态的数据
			pageInfo = this.manageCommentsApi.queryMomentsListAllByData(page, pageSize, sortProp, sortOrder, start, end);
		}
		
		if (code.equals("3")) {
			// 查询状态为人工审核的数据
			pageInfo = this.manageCommentsApi.queryMomentsListReviewByData(page, pageSize, sortProp, sortOrder, start, end);
		}
		
		if (code.equals("4")) {
			// 查询状态为拒绝的数据
			pageInfo = this.manageCommentsApi.queryMomentsListRejectByData(page, pageSize, sortProp, sortOrder, start, end);
		}
		
		if (code.equals("5")) {
			// 查询状态为通过的数据
			pageInfo = this.manageCommentsApi.queryMomentsListPassByData(page, pageSize, sortProp, sortOrder, start, end);
		}
		
		// 获取到动态publish集合,遍历强化封装成moment
		List<Moments> records = pageInfo.getRecords();
		List<MomentsVo> momentsVos = new ArrayList<>();
		for (Moments record : records) {
			MomentsVo momentsVo = new MomentsVo();
			momentsVo.setUserId(record.getUserId());  // 设置用户ID
			// List<String> medias = record.getMedias();
			momentsVo.setCreateDate(record.getCreateDate()); // 设置动态创建时间
			momentsVo.setMedias(record.getMedias());  // 设置medias
			momentsVo.setText(record.getText()); // 设置文本内容
			
			if (record.getState().equals("review")) {
				momentsVo.setState("3");
			}
			if (record.getState().equals("pass") || record.getState().equals("auto_pass")) {
				momentsVo.setState("5");
			}
			if (record.getState().equals("auto_reject") || record.getState().equals("reject")) {
				momentsVo.setState("4");
			}
			
			// 获得相应id用户的详细信息,进行设置头像和昵称
			QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
			queryWrapper.eq("user_id", record.getUserId());
			UserInfo userInfo = this.userInfoMapper.selectOne(queryWrapper);
			
			momentsVo.setUserLogo(userInfo.getLogo()); // 设置头像
			momentsVo.setNickname(userInfo.getNickName()); // 设置昵称
			
			momentsVo.setId(record.getOid().toHexString()); // 设置动态ID
			momentsVo.setReportCount(RandomUtils.nextInt(50, 80)); // 设置举报数量
			momentsVo.setLikeCount(RandomUtils.nextInt(50, 500)); // 设置点赞数量
			momentsVo.setCommentCount(RandomUtils.nextInt(0, 50)); // 设置评论数量
			momentsVo.setForwardingCount(RandomUtils.nextInt(0, 50)); // 设置转发数量
		}
		
		if (pageInfo.getTotal() % pageInfo.getPageSize() == 0) {
			pageResult.setPages(pageInfo.getTotal() / pageInfo.getPageSize());
		} else {
			pageResult.setPages(pageInfo.getTotal() / pageInfo.getPageSize() + 1);
		}
		
		pageResult.setCounts(pageInfo.getTotal());
		
		pageResult.setItems(momentsVos);
		
		return pageResult;
	}
	
	public void momentPass(String[] ids) {
		for (String id : ids) {
			// 遍历动态id集合中的每个id,获取moments表中id对应的数据,将其审核状态值更改为5:通过,并进行三表发布
			Moments moments = this.manageCommentsApi.findByPid(id);
			Publish publish = new Publish();
			publish.setId(moments.getOid());
			publish.setPid(moments.getPid());
			publish.setUserId(moments.getUserId());
			publish.setText(moments.getText());
			publish.setMedias(moments.getMedias());
			publish.setSeeType(moments.getSeeType());
			publish.setSeeList(moments.getSeeList());
			publish.setNotSeeList(moments.getNotSeeList());
			publish.setLatitude(moments.getLatitude());
			publish.setLongitude(moments.getLongitude());
			publish.setLocationName(moments.getLocationName());
			publish.setCreated(moments.getCreateDate());
			
			// -------------------------1、写入发布表----------------------------------------------
			// 保存动态信息
			this.mongoTemplate.save(publish);
			
			// --------------------------2、写入自己的相册表中----------------------------------------
			// 写入自己的相册表中
			Album album = new Album();
			album.setId(ObjectId.get());
			// 朋友圈动态id
			album.setPublishId(publish.getId());
			// 发布时间
			album.setCreated(System.currentTimeMillis());
			// 写入mongodb中, 表名为moments_album_{id}
			this.mongoTemplate.save(album, "quanzi_album_" + publish.getUserId());
			
			// --------------------------3、写入好友时间线中------------------------------------------------
			// 查询当前用户的好友数据，将动态数据写入到好友的时间线表中
			Criteria criteria = Criteria.where("userId").is(publish.getUserId());
			Query query = Query.query(criteria);
			List<Users> users = this.mongoTemplate.find(query, Users.class);
			for (Users user : users) {
				TimeLine timeLine = new TimeLine();
				timeLine.setId(ObjectId.get());
				timeLine.setPublishId(publish.getId());
				timeLine.setUserId(user.getUserId());
				timeLine.setDate(System.currentTimeMillis());
				this.mongoTemplate.save(timeLine, "quanzi_time_line_" + user.getFriendId());
			}
			
			// 修改审核状态
			this.manageCommentsApi.passByPid(id);
		}
	}
	
	public void momentReject(String[] ids) {
		for (String id : ids) {
			// 修改审核状态
			this.manageCommentsApi.RejectByPid(id);
		}
	}
	
	public void momentRevocation(String[] ids) {
		// 将审核状态更改为待审核状态,并将该条动态从三表中删除
		for (String id : ids) {
			// 修改审核状态
			this.manageCommentsApi.RevocationByPid(id);
			
			// 从三表中删除
			Query query = Query.query(Criteria.where("id").is(id));
			this.mongoTemplate.remove(query, Publish.class);
			this.mongoTemplate.remove(query, Album.class);
			this.mongoTemplate.remove(query, TimeLine.class);
		}
	}
	
	public DetailsVo findByMsgId(String id) {
		Query query = Query.query(Criteria.where("oid").is(id));
		// 从MongoDB中获取到所有发布的动态
		Moments moment = this.mongoTemplate.findOne(query, Moments.class);
		DetailsVo detailsVo = new DetailsVo();
		
		detailsVo.setUserId(moment.getUserId());  // 设置用户ID
		detailsVo.setCreateDate(moment.getCreateDate()); // 设置动态创建时间
		detailsVo.setMedias(moment.getMedias());  // 设置medias
		detailsVo.setText(moment.getText()); // 设置文本内容
		detailsVo.setId(moment.getOid().toHexString()); // 设置动态ID
		detailsVo.setReportCount(RandomUtils.nextInt(50, 80)); // 设置举报数量
		detailsVo.setLikeCount(RandomUtils.nextInt(50, 500)); // 设置点赞数量
		detailsVo.setCommentCount(RandomUtils.nextInt(0, 50)); // 设置评论数量
		detailsVo.setForwardingCount(RandomUtils.nextInt(0, 50)); // 设置转发数量
		if (moment.getState().equals("review")) {
			detailsVo.setState("3");
		}
		if (moment.getState().equals("pass") || moment.getState().equals("auto_pass")) {
			detailsVo.setState("5");
		}
		if (moment.getState().equals("auto_reject") || moment.getState().equals("reject")) {
			detailsVo.setState("4");
		}
		
		// 获得相应id用户的详细信息,进行设置头像和昵称
		QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("user_id", moment.getUserId());
		UserInfo userInfo = this.userInfoMapper.selectOne(queryWrapper);
		
		detailsVo.setUserLogo(userInfo.getLogo()); // 设置头像
		detailsVo.setNickname(userInfo.getNickName()); // 设置昵称
		
		detailsVo.setTopState("2"); // 设置指定状态
		
		return detailsVo;
	}
}
